package com.djc8.controller.admin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.djc8.entity.Page;
import com.djc8.entity.UserInfoBean;
import com.djc8.model.Model;


/**
 * Servlet implementation class GetUserList
 */
public class GetUserList extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String page=request.getParameter("page");
		
		String row=request.getParameter("rows");
		
		Model model=new Model();
		Page p=null;
		if(page!=null && row!=null && !page.equals("") && !row.equals("") )
		{
			p=model.doPage(Integer.parseInt(page), Integer.parseInt(row));
		}
		else
		{
			p=model.doPage(1, 10);
		}
		
		List<UserInfoBean> list=new ArrayList<UserInfoBean>(p.getPageList());
		JSONArray jsonArray=new JSONArray();
		for (UserInfoBean bean : list) {
			JSONObject obj=new JSONObject();
			obj.put("usId", bean.getUsId());
			obj.put("userName",bean.getUserName());
			obj.put("uiEmail", bean.getUiEmail());
			obj.put("uiNice", bean.getUiNice());
			obj.put("uiNote", bean.getUiNote());
			//obj.put("uiLoginDate", bean.getUiLoginDate());
			//obj.put("uiRegDate", bean.getUiRegDate());
			jsonArray.add(obj);
		}
		JSONObject obj=new JSONObject();
		obj.put("total", p.getTotalNum());
		obj.put("rows", jsonArray);
		
		//jsonArray.add(new UserInfoBean(0,0,"1","1","2","3","4",null,null));
		
		response.getWriter().print(obj.toString());
		//System.out.println(jsonArray.toString());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}
